package com.transaction.demo.message.biz.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.transaction.demo.common.util.ResultPage;
import com.transaction.demo.message.biz.TransactionMessageBiz;
import com.transaction.demo.message.dao.TransactionMessageDao;
import com.transaction.demo.message.entity.TransactionMessageBean;
import com.transaction.demo.message.vo.req.TransactionMessageAddReqVo;
import com.transaction.demo.message.vo.req.TransactionMessageListReqVo;
import com.transaction.demo.message.vo.req.TransactionMessageUpdateReqVo;
import com.transaction.demo.message.vo.req.WaitOrSendMessageListReqVo;
import com.transaction.demo.message.vo.resp.TransactionMessageRespVo;

/**
 * 描述：
 * 作者：马庆智
 * 日期：2023/04/26 10:29:01
 */
@Service
public class TransactionMessageBizImpl implements TransactionMessageBiz {

	@Autowired
	private TransactionMessageDao transactionMessageDao;

	/**
	 * 查询
	 *
	 * @param messageId 请求参数
	 * @return TransactionMessageResp
	 * @author 马庆智
	 * @date: 2023/04/26 10:29:01
	 */
	@Override
	public TransactionMessageRespVo queryTransactionMessage(String messageId) {

		TransactionMessageBean messageBean = transactionMessageDao.queryTransactionMessage(
				TransactionMessageBean.builder().messageId(messageId).build());

		TransactionMessageRespVo messageRespVo = new TransactionMessageRespVo();

		BeanUtils.copyProperties(messageBean, messageRespVo);
		return messageRespVo;
	}

	/**
	 * 查询列表
	 *
	 * @param msg 请求参数
	 * @return TransactionMessageResp
	 * @author 马庆智
	 * @date: 2023/04/26 10:29:01
	 */
	@Override
	public ResultPage<TransactionMessageRespVo> queryTransactionMessageList(TransactionMessageListReqVo msg) {

		PageHelper.startPage(msg.getCurrent(), msg.getPageSize());
		List<TransactionMessageBean> query = transactionMessageDao.queryTransactionMessageList(
				TransactionMessageBean.builder().messageId(msg.getMessageId()).consumerQueue(msg.getConsumerQueue())
						.areadlyDead(msg.getAreadlyDead()).status(msg.getStatus()).build());
		PageInfo<TransactionMessageBean> pageInfo = new PageInfo<>(query);

		List<TransactionMessageRespVo> list = pageInfo.getList().stream().map(x -> {
			TransactionMessageRespVo resp = new TransactionMessageRespVo();
			BeanUtils.copyProperties(x, resp);
			return resp;
		}).collect(Collectors.toList());

		return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());

	}

	/**
	 * 添加
	 *
	 * @param transactionMessage 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/26 10:29:01
	 */
	@Override
	public int saveTransactionMessage(TransactionMessageAddReqVo transactionMessage) {

		TransactionMessageBean messageBean = new TransactionMessageBean();
		BeanUtils.copyProperties(transactionMessage, messageBean);

		return transactionMessageDao.saveTransactionMessage(messageBean);
	}

	/**
	 * 删除
	 *
	 * @param messageId 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/26 10:29:01
	 */
	@Override
	public int deleteTransactionMessage(String messageId) {
		return transactionMessageDao.deleteTransactionMessage(messageId);
	}

	/**
	 * 更新
	 *
	 * @param transactionMessage 请求参数
	 * @return int
	 * @author 马庆智
	 * @date: 2023/04/26 10:29:01
	 */
	@Override
	public int updateTransactionMessage(TransactionMessageUpdateReqVo transactionMessage) {
		TransactionMessageBean messageBean = new TransactionMessageBean();
		BeanUtils.copyProperties(transactionMessage, messageBean);
		return transactionMessageDao.updateTransactionMessage(messageBean);
	}

	/**
	 * 查询待确认或者发送中的的消息
	 *
	 * @param record 请求参数
	 * @return TransactionMessageResp
	 * @author 马庆智
	 * @date: 2023/04/26 10:29:01
	 */
	@Override
	public ResultPage<TransactionMessageRespVo> queryWaitOrSendMessageList(WaitOrSendMessageListReqVo record) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = dateFormat.parse(record.getCreateTimeBefore());
			PageHelper.startPage(record.getCurrent(), record.getPageSize());
			List<TransactionMessageBean> query = transactionMessageDao.queryTransactionMessageList(
					TransactionMessageBean.builder().status(record.getStatus()).createTime(date)
							.areadlyDead(record.getAreadlyDead()).build());
			PageInfo<TransactionMessageBean> pageInfo = new PageInfo<>(query);

			List<TransactionMessageRespVo> list = pageInfo.getList().stream().map(x -> {
				TransactionMessageRespVo resp = new TransactionMessageRespVo();
				BeanUtils.copyProperties(x, resp);
				return resp;
			}).collect(Collectors.toList());

			return new ResultPage<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), pageInfo.getTotal());
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}

	}
}